package com.quantstack.llcommonlibrary.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.hjq.toast.ToastUtils;
import com.quantstack.llcommonlibrary.dialog.LoadingDialog;
import com.quantstack.llcommonlibrary.utils.ActivityUtil;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/2/22 17:58
 * @Notes 只有显示没有数据绑定
 */
public abstract class BaseNullActivity<VDB extends ViewDataBinding> extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    protected VDB mViewDataBind;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //    getWindow().getDecorView().setSystemUiVisibility(
        //            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        //                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        //    getWindow().setStatusBarColor(Color.TRANSPARENT);
        //}
        //setContentView(getLayoutId());
        mViewDataBind = DataBindingUtil.setContentView(this, getLayoutId());
        mViewDataBind.setLifecycleOwner(this);
        //加入管理
        ActivityUtil.getInstance().addActivity(this);
        //addStatusViewWithColor(this,getResources().getColor(R.color.theme_color));
        afterCreate();
    }

    /**
     * 设置沉浸式状态栏头部间距
     *
     * @param activity
     */
    public void addStatusViewWithColor(Activity activity, int Color){
        //设置 paddingTop
        ViewGroup rootView = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);
        rootView.setPadding(0, getStatusBarHeight(), 0, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 以上直接设置状态栏颜色
            activity.getWindow().setStatusBarColor(Color);
        } else {
            //根布局添加占位状态栏
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            View statusBarView = new View(activity);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight());
            statusBarView.setBackgroundColor(Color);
            decorView.addView(statusBarView, lp);
        }
    }

    /**
     * 利用反射获取状态栏高度
     * @return
     */
    private int getStatusBarHeight() {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    //获取布局
    public abstract int getLayoutId();
    public abstract void afterCreate();


    /**
     * 直接跳转Activity
     * @param activity
     * @param cls
     */
    public void go(Activity activity,Class cls){
        Intent intent = new Intent(activity,cls);
        activity.startActivity(intent);
    }

    /**
     * 携带数据跳转Activity
     * @param activity
     * @param cls
     */
    public void go(Activity activity,Class cls,Bundle bundle){
        Intent intent = new Intent(activity,cls);
        activity.startActivity(intent,bundle);
    }

    /**
     * 携带返回值数据跳转Activity
     * @param activity
     * @param cls
     */
    public void go(Activity activity, Class cls, int resultCode){
        Intent intent = new Intent(activity,cls);
        activity.startActivityForResult(intent,resultCode);
    }

    /**
     * 携带返回值数据跳转Activity
     * @param activity
     * @param cls
     */
    public void go(Activity activity, Class cls, Bundle bundle, int resultCode){
        Intent intent = new Intent(activity,cls);
        activity.startActivityForResult(intent,resultCode,bundle);
    }

    /**
     * 封装toast方法（自行去实现）
     *
     * @param str
     */
    public void showToast(String str) {
        ToastUtils.show(str);
    }

    public void showLongToast(String str) {
        ToastUtils.show(str);
    }

    public void showLoadingDialog() {
        showLoadingDialog("加载中...");
    }
    /**
     * 消失  黑框...
     */
    public void dissMissDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    public void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        loadingDialog.setMessage(msg);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }
}
